<?php
namespace app\mysql\controller;  // 必须继承于app 第二个是具体的应用名  例 t5/note/tp
use think\Controller;
//use think\Request;

use app\mysql\model\Listt;  //必须继承于app 第二个是具体的应用名  例 t5/note/tp 第三个首字母大写
use app\mysql\model\Test;  //必须继承于app 第二个是具体的应用名  例 t5/note/tp 第三个首字母大写
use think\config;


// use think\Db;
class Index extends Controller
{

	
    public function index()
    {
		$model = new Test();
		$model->index();
	echo 'mysql/index';

	}
    /**
	 * 插入 新增数据 返回影响了一条数据
	 */

	public function index2(){
		$val1 = "'老赵'";  //如果是文字 必须双引号 套 单引号
		$val2 = 145;     // 输入数值型
		$name = 'name,age';     // 插入的字段名
	   
	    $val = $val1.','.$val2;  // 拼接插入的值  类似 'xiaowang','2855'
		$model = new Test();
		$data = $model->index2($name,$val);
		$this->index3();
		echo Test::getlastsql();  // model 下获得最后一条sql
		dump($data);
	}

	public function index3(){
		echo'
		<hr/>
		<pre>
		$val1 = "\'老赵\'";  //如果是文字 必须双引号 套 单引号
		$val2 = 145;     // 输入数值型
		$name = \'name,age\';     // 插入的字段名
	   
		$val = $val1.\',\'.$val2;  // 拼接插入的值  类似 \'xiaowang\',\'2855\'
	
		$model = new Test();
		$data = $model->index2($name,$val);
		echo Test::getlastsql();  // model 下获得最后一条sql
		<br/><br/><br/><br/>
		model 层
		return $this->execute("insert into note_test ($name)values($val)");
}
		</pre>
		<hr/>
		';
	}

	/**
	 * 查询 所有字段 全部查询
	 */
	public function index4(){
		$model = new Test();
		$data = $model->index3();
		 $this->index5();
		echo Test::getlastsql();  // model 下获得最后一条sql
		dump($data);
	}

	public function index5(){
		echo'
		<hr/>
		<pre>
		$model = new Test();
		$data = $model->index3();
		echo Test::getlastsql();  // model 下获得最后一条sql
		<br/><br/><br/><br/>
		model 层
		return $this->query("select * from note_test");
		</pre>
		<hr/>
		';
	}


     /**
	 * 查询 指定字段 部分字段
	 */
	public function index6(){

		$name = 'name,age,height';
		$model = new Test();
		$data = $model->index4($name);
		 $this->index7();
		echo Test::getlastsql();  // model 下获得最后一条sql
		dump($data);
	}

	public function index7(){
	   echo'
	   <hr/>
	   <pre>
		$name = \'name,age,height\';
		$model = new Test();
		$data = $model->index4($name);
	
		echo Test::getlastsql();  // model 下获得最后一条sql
		<br/><br/><br/><br/>
		model 层
		return $this->query("select $name from note_test where 1");
		</pre>
		<hr/>
		';
	}

     /**
	 * 查询 条件查询 跨表操作 条件拼接
	 */
	public function index8(){
		$logic = '>=';  // 逻辑
		$val = 576;      
		$table = "note_listt";
		$where = "id".$logic.$val;  // 拼出条件  id>=75

		$model = new Test();
		$data = $model->index5($table,$where);
		 $this->index9();
		echo Test::getlastsql();  // model 下获得最后一条sql
		dump($data);
	
	}

	public function index9(){
		echo'
		<hr/>
		<pre>
		$logic = \'>=\';  // 逻辑
		$val = 576;      
		$table = "note_listt";
		$where = "id".$logic.$val;  // 拼出条件  id>=75

		$model = new Test();
		$data = $model->index5($table,$where);
		
		echo Test::getlastsql();  // model 下获得最后一条sql
		<br/><br/><br/><br/>
		model 层
		return $this->query("select * from $table where $where");
		</pre>
		<hr/>
		';
	}

     /**
	 * 删除数据 条件删除 返回 影响了多少条数据
	 */
	public function index10(){
		//组装where 条件
		$name = 'age';
		$logic = ">";
		$val = 140;
		$where = $name.$logic.$val; 
		$table = "note_test";
		$model = new Test();
		$data = $model->index6($table,$where);
		$this->index11();
		echo Test::getlastsql();  // model 下获得最后一条sql
		dump($data);
    }

	public function index11(){
		echo'
		<hr/>
		<pre>
		//组装where 条件
		$name = \'age\';
		$logic = ">";
		$val = 140;
		$where = $name.$logic.$val; 
		$table = "note_test";
		$model = new Test();
		$data = $model->index6($table,$where);
		<br/><br/><br/><br/>
		model 层
		return $this->execute("delete from $table where $where");
		</pre>
		<hr/>
		';
    }

     /**
	 * 修改数据 有条件 返回 影响了多少条数据
	 */
	public function index12(){
		//组装where 条件
		$name = 'name';
		$logic = "=";
		$val = "'找事'";

		$where = $name.$logic.$val; 
		
		//组装修改的内容
		$updata_name = 'address';
		$updata_val = "'北京市东城区'";
		$up_data = $updata_name.'='.$updata_val;  //拼接修改的内容
		
		$table = "note_test";
		$model = new Test();
		$data = $model->index7($table,$where,$up_data);
		$this->index13();
		echo Test::getlastsql();  // model 下获得最后一条sql
		dump($data);
	}
	
	public function index13(){
		echo'
		<hr/>
		<pre>
		//组装where 条件
		$name = \'name\';
		$logic = "=";
		$val = "\'找事\'";

		$where = $name.$logic.$val; 
		
		//组装修改的内容
		$updata_name = \'address\';
		$updata_val = "\'北京市东城区\'";
		$up_data = $updata_name.\'=\'.$updata_val;  //拼接修改的内容
		
		$table = "note_test";
		$model = new Test();
		$data = $model->index7($table,$where,$up_data);
		<br/><br/><br/><br/>
		model 层
		return $this->execute("update $table set $up_data where $where");

		</pre>
		<hr/>
		';
	}


    /**
	 * 排序
	 */
	public function index14(){
		
		$name = 'age';
		$table = "note_test";
		$model = new Test();
		$data = $model->index8($table,$name);
		$this->index15();
		echo Test::getlastsql();  // model 下获得最后一条sql
		dump($data);
	}

	public function index15(){
		echo'
		<hr/>
		<pre>
		$name = \'age\';
		$table = "note_test";
		$model = new Test();
		$data = $model->index8($table,$name);
		<br/><br/><br/><br/>
		model 层
		return $this->query("select * from $table order by $name");

		<pre>
		<hr/>
		';
	}

     /**
	 * 原生sql 查询 联合 union  返回查询的数据
	 */
	public function index16(){
	
		$table = "note_listt";
		
		$model = new Test();
		$data = $model->index9();
		// $this->index9();
	//	echo Test::getlastsql();  // model 下获得最后一条sql
		dump($data);
	
	}


/**
 * 批量新增 数据来源：数组
 * http://www.hyhthjk.com.cn/note.php/mysql/Index/index17
 */
public function index17(){
	$data = array(
		'0' =>array('老张',25,176),
		'1' =>array('批量新增',23,168),
		'2' =>array('新增3',15,155)
	);
	$data1 = "'新增4',25,175";
// dump($data);
$name = 'name,age,height';
$val = $data1;
$model = new Test();
$data = $model->index2($name,$val);
echo Test::getlastsql();

}




 /***
  * 批量新增 数据来源 ：表单
  */



	//在表一的model 层 操作表二
		}

